import path from 'path'
import fs from 'fs-extra'
import Creator from './Creator.js'
import loading from './util.js'
import Inquirer from 'inquirer'
import chalk from 'chalk'

export default async function (projectName, options) {
    // 获取当前工作目录
    const cwd = process.cwd();
    const targetDirectory = path.join(cwd, projectName);

    if (fs.pathExistsSync(targetDirectory)) {
        if (options.force) {
            // 删除重名目录
            await fs.remove(targetDirectory);
        } else {
            let {isOverwrite} = await new Inquirer.prompt([
                {
                    name: "isOverwrite",
                    type: "confirm",
                    message: "target directory exists, overwrite target directory?",
                }
            ])

            if (!isOverwrite) {
                console.log(chalk.red(`${projectName} is not overwritten.`))
                return;
            } else {
                await loading(
                    `Removing ${projectName}, please wait...`,
                    fs.remove,
                    targetDirectory,
                )

            }

        }
    }

    // 创建项目
    const creator = new Creator(projectName, targetDirectory);
    creator.create(projectName);
}